/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trefliq.EJB.sessionbeans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import trefliq.EJB.entities.Genre;

/**
 *
 * @author Yash
 */
@Stateless
public class GenreBean extends AbstractFacade<Genre> {
    @PersistenceContext(unitName = "trefliqPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public GenreBean() {
        super(Genre.class);
    }
    
    public boolean createGenre(String genre){
         try{
             Genre g = new Genre(genre);
             em.persist(g);
             return true;
         }
         catch(Exception e){
             System.out.println("Cound not create new genee :"+genre);
             System.out.println(e);
             return false;
         }
     }
     
    // AbstractFacade methods.  Copied in to apply security attributes.
    
    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void create(Genre entity) {
        super.create(entity);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void edit(Genre entity) {
        super.edit(entity);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void remove(Genre entity) {
        super.remove(entity);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public Genre find(Object id) {
        return super.find(id);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public List<Genre> findAll() {
        return super.findAll();
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public List<Genre> findRange(int[] range) {
        return super.findRange(range);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public int count() {
        return super.count();
    }

    
    public List<Genre> getAllGenre(){
        List<Genre> genreList = null;
        try{
             genreList = em.createNamedQuery("findAllGenres" ,Genre.class).getResultList();
        }catch(Exception e){
            System.err.println("Could not get all generes! "+e);
        }
        return genreList;
    }
    
    public Genre getGenreById(Long id){
         return em.createNamedQuery("findById", Genre.class).
                    setParameter("id", id).getSingleResult();
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void deleteGenreByName(String gName){
        try{
            em.createNamedQuery("removeGenreByName", Genre.class).setParameter("gName", gName);
            System.out.println("Genre: "+gName+" is removed!");
        }catch(Exception e){
            System.err.println("Could not remove genere: "+gName+" -> "+e);
        }
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public void updateGenreByName(String oldName, String newName){
        try{
            Query q = em.createNamedQuery("updateGenres", Genre.class);
            q.setParameter("oldName", oldName);
            q.setParameter("newName", newName);
            System.out.println("Genre name "+oldName+" is changed to "+newName);
        }catch(Exception e){
            System.err.println("Could genere name: "+oldName+" -> "+e);
        }
    }
}
